package com.ocean.admin.extension.security.core;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.ocean.admin.entity.SysUser;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Set;

/**
 * 用户
 *
 * @author zhang
 * @since 2023/11/24
 */
@EqualsAndHashCode(callSuper = true)
@JsonIgnoreProperties({"enabled", "accountNonExpired", "accountNonLocked", "credentialsNonExpired", "authorities"})
@Data
public class SysUserDetails extends SysUser implements UserDetails {

    /**
     * 角色对象
     */
    private Set<String> roles;

    /**
     * 用户权限
     */
    private Set<String> permissions;

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        List<SimpleGrantedAuthority> simpleAuthorities = new ArrayList<>();
        for (String authority : this.permissions) {
            simpleAuthorities.add(new SimpleGrantedAuthority(authority));
        }
        return simpleAuthorities;
    }

    @Override
    public String getUsername() {
        return getUserName();
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }
}
